//
//  RW_PushPlayersTools.m
//  SleepApp
//
//  Created by chuangqi on 2021/4/2.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_PushPlayersTools.h"
#import "RW_PlayerViewController.h"
#import "RW_MixedViewController.h"
#import "UIView+BlockGesture.h"
#import "TabBarController.h"

/** 播放类型Key */
static NSString * PlayersType_Key = @"PlayersType_Key";


static RW_PushPlayersTools *class = nil;
@interface RW_PushPlayersTools ()
/** 悬浮图标 */
@property (nonatomic,strong) UIImageView *play_icon_view;
/** 悬浮图标的状态 */
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation RW_PushPlayersTools
#pragma mark -- 单利方法重写 --
+ (id)allocWithZone:(struct _NSZone *)zone{
    if (!class) {
        class = [super allocWithZone:zone];//如果没有实例让父类去创建一个
        return class;
    }
    return nil;
}
- (id)copyWithZone:(struct _NSZone *)zone{
    return self;
}
+ (instancetype) singletonMod {
    @synchronized(self) {
        if (class == nil) {
            class = [[self alloc] init];
            
        }
    }
    return class;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self timer];
    }
    return self;
}
-(void)addPlayIconFromVc:(UIViewController *)vc{
    [vc.view addSubview:self.play_icon_view];
    [self.play_icon_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(vc.view).mas_offset(-20);
        make.bottom.mas_equalTo(vc.view).mas_offset( -kTabBarSafeHeight - 45);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
}
-(void)runTime{
    BOOL isPlaying = ([RW_PlayerViewController playingStatus] ||
                      [RW_MixedViewController playingStatus]);
    self.play_icon_view.hidden = !isPlaying;
}
#pragma mark -- 懒加载
-(NSTimer *)timer{
    if (!_timer){
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(runTime) userInfo:nil repeats:YES];
    }
    return _timer;
}
-(UIImageView *)play_icon_view{
    if (!_play_icon_view){
        _play_icon_view = [[UIImageView alloc]initWithImage:[UIImage svgName:@"public_play_icon"]];
        _play_icon_view.hidden = YES;
        [_play_icon_view addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            [RW_PushPlayersTools pushSingleFromVc:showVC];
        }];
    }
    return _play_icon_view;
}

#pragma mark -- 默认播放器 --
/** 获取当前默认播放器类型 */
+(Players_Type)getPlayersType{
    return Players_Type_Player;
//    return (Players_Type)[UserDefaults_wl integerForKey:PlayersType_Key];
}
/** 设置当前默认播放器类型 */
+(void)setPlayersType:(Players_Type)players_type{
    [UserDefaults_wl setInteger:players_type forKey:PlayersType_Key];
    [UserDefaults_wl synchronize];
}

#pragma mark -- 公共功能 --
/** 关闭其他播放器的播放状态 */
+(void)stopOtherPlay{
    [RW_PlayerViewController stopPlayerAudio];
    [RW_MixedViewController stopMixedAudio];
}

#pragma mark -- 跳转方法 --
/** 打开当前播放列表 */
+(void)pushSingleFromVc:(UIViewController *)fromVc{
    [self openPlayers:^(Players_Type players_type) {
        if (players_type == Players_Type_Player){
            [RW_PlayerViewController pushSingleFromVc:fromVc];
        } else if (players_type == Players_Type_Mixed){
            [RW_MixedViewController pushSingleFromVc:fromVc];
        }
    }];
}
/** 打开公共播放列表 */
+(void)pushSingleId:(NSString *)singleId fromVc:(UIViewController *)fromVc{
    [self openPlayers:^(Players_Type players_type) {
        if (players_type == Players_Type_Player){
            [RW_PlayerViewController pushSingleId:singleId fromVc:fromVc];
        } else if (players_type == Players_Type_Mixed){
            [RW_MixedViewController pushSingleId:singleId fromVc:fromVc];
        }
    }];
}
/** 打开个人播放列表 */
+(void)pushUserSingleId:(NSString *)singleId fromVc:(UIViewController *)fromVc{
    [self openPlayers:^(Players_Type players_type) {
        if (players_type == Players_Type_Player){
            [RW_PlayerViewController pushUserSingleId:singleId fromVc:fromVc];
        } else if (players_type == Players_Type_Mixed){
            [RW_MixedViewController pushUserSingleId:singleId fromVc:fromVc];
        }
    }];
}
/** 打开单个歌曲播放 */
+(void)pushSongId:(NSString *)songId fromVc:(UIViewController *)fromVc{
    [self openPlayers:^(Players_Type players_type) {
        if (players_type == Players_Type_Player){
            [RW_PlayerViewController pushSongId:songId fromVc:fromVc];
        } else if (players_type == Players_Type_Mixed){
            [SVProgressHUD showError_wl:@"混声播放器不支持单个音频播放"];
        }
    }];
}


/** 区分播放器类型 */
+(void)openPlayers:(void(^)(Players_Type players_type))openBlock{
    if ([self getPlayersType] == Players_Type_Default){
        
    } else {
        if (openBlock){
            openBlock([self getPlayersType]);
        }
    }
}
@end
